//
//  ReplayMeVC.m
//  TuYouAPP
//
//  Created by L on 14-3-14.
//
//

#import "ReplayMeVC.h"
#import "MyReplyCell.h"
#import "UIImageView+WebCache.h"
#import "DynamicReplyVC.h"
@interface ReplayMeVC ()
{
        NSMutableArray  *tempArray;
}

@end

@implementation ReplayMeVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)loadData
{
    [SVProgressHUD showWithStatus:@"加载中"];
    self.page = 1;
    [[ TYUser currentUser]getReplyMepage:self.page call:^(NSArray *arr, NSString *error) {
          [self.slimeView endRefresh];
        self.tableview.tableFooterView.hidden = NO;
        if (!error && arr) {
            [SVProgressHUD dismiss];
            if (arr.count !=ONE_PAGE) {
                self.refreshLabel.text = self.textNoMore;
            }
            else
            {
                 self.refreshLabel.text = self.textPull;
            }
            self.page++;
            tempArray = [[NSMutableArray alloc]init];
            [tempArray addObjectsFromArray:arr];
            [self.tableview reloadData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:error];
        }
        
    }];
}

- (void)viewDidLoad
{
    self.mPageName = @"评论我的";

    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self loadData];
    
    
	// Do any additional setup after loading the view.
    self.Title = @"评论我的";
    self.LeftBarTitle = @"我的";
    self.haveFullupRefreshView = YES;
    
    [self initWithRefreshTableViewIos7:CGRectMake(0, 64, 320, DEVICE_Height-64) Ios6rect:CGRectMake(0, 44, 320, DEVICE_Height-64)];
    self.tableview.tableFooterView.hidden = YES;
    
    self.tableview.backgroundColor = ColorRGB(230, 230, 230);
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableview];

    [[TYUser currentUser] clearFlag:E_flag_have_reply];
    
}

-(void)leftBtnTouched:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tempArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"MyReplyCell";
    
    MyReplyCell *cell = (MyReplyCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    
    BOOL isUsed = NO;
    if (!isUsed &&! cell) {
        UINib *nib = [UINib nibWithNibName:@"MyReplyCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:@"MyReplyCell"];
        // isUsed = YES;
        cell = (MyReplyCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
        
    }
    
    TYCommment *data = [tempArray objectAtIndex:indexPath.row];
    NSMutableString *first = [[NSMutableString alloc]initWithFormat:@"@%@:",data.mUserName ];
    [first appendString:data.mText];
    [cell upDateCell:first content:data.mWhichIComent.mContext];
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:first];
    
    [str addAttribute:NSForegroundColorAttributeName value:ColorRGB(205, 136, 52) range:NSMakeRange(0,data.mUserName.length+1)];
    
    NSInteger strLength = [[str string] length];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:2];
    
    [str addAttribute:NSParagraphStyleAttributeName
                value:style
                range:NSMakeRange(0, strLength)];
    
    //  cell.publishContent.attributedText = attString;
    
    
    cell.firstReply.attributedText = str;
    
    cell.myView.layer.borderWidth = 0.5;
    cell.myView.layer.borderColor = [ColorRGB(212, 212, 212)CGColor];
    
    cell.contentName.text = data.mWhichIComent.mTaguser.mUserDisplayName;
    NSString *pingluncount = data.mWhichIComent.mReBackCount!=0? [NSString stringWithFormat:@"%d条回复",data.mWhichIComent.mReBackCount]:@"暂无回复";
    cell.pinglunCount.text = pingluncount;
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:data.mWhichIComent.mContext];
    NSInteger astrLength = [data.mWhichIComent.mContext length];
    NSMutableParagraphStyle *astyle = [[NSMutableParagraphStyle alloc] init];
    [astyle setLineSpacing:2];
    [attString addAttribute:NSParagraphStyleAttributeName
                      value:astyle
                      range:NSMakeRange(0, astrLength)];
    
    cell.content.attributedText = attString;
    cell.replyTime.text = data.mTime;
    [cell.headImage setImageWithURL:[NSURL URLWithString:data.mWhichIComent.mTaguser.mHeadImgURL] placeholderImage:[UIImage imageNamed:@"70_70.png"]];
    //  cell.firstReply.text  = data.myReply;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // cell.textLabel.text = @"success";
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height+1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TYCommment *state = [tempArray objectAtIndex:indexPath.row];
    DynamicReplyVC *vc = [[DynamicReplyVC alloc]init];
    vc.thePublish = state.mWhichIComent;
    [self.navigationController pushViewController:vc animated:YES];
    
    
}
-(void)slimeRefreshStartRefresh:(SRRefreshView *)refreshView
{
    [self loadData];
    
}
-(void)refresh
{
    
    [[ TYUser currentUser]getReplyMepage:self.page call:^(NSArray *arr, NSString *error) {
        
        if (tempArray.count==0) {
           // [SVProgressHUD showErrorWithStatus:@"没有更多数据"];
            self.refreshLabel.text = self.textNoMore;
            [self stopLoading];
            return;
        }
        [self stopLoading];
        [SVProgressHUD dismiss];
        self.tableview.tableFooterView.hidden = NO;
        
        if (!error && arr) {
            self.page++;
            if (arr.count == 0) {
              //  [SVProgressHUD showErrorWithStatus:@"没有更多数据"];
                self.refreshLabel.text = self.textNoMore;
                
                return ;
            }
            //  tempArray = [[NSMutableArray alloc]init];
            [tempArray addObjectsFromArray:arr];
            [self.tableview reloadData];
            if (arr.count <ONE_PAGE) {
                self.refreshLabel.text = self.textNoMore;
            }
            else
            {
                self.refreshLabel.text = self.textPull;
                
            }
            
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:error];
        }
        
    }];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
